package wechat.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import wechat.resources.InstanceResource;
import wechat.service.MessageService;
import wechat.service.base.BaseRequest;

@Service
public class IMessageService extends BaseRequest implements MessageService {

	private Logger log = LoggerFactory.getLogger(IMessageService.class);
	
	public static final String Message_Text = "text";
	public static final String Message_Image = "image";
	public static final String Message_Voice = "voice";
	public static final String Message_Video = "video";
	public static final String Message_Music = "music";
	public static final String Message_News = "news";
	
	@Autowired
	IAccessTokenService accessTokenService;
	
	
	public String sendText(InstanceResource instanceResource,JSONObject jsonParam) throws Exception{
		log.info("IMessageService -- sendText  jsonParam=" + jsonParam);
		String access_token = accessTokenService.getAccessToken(instanceResource).getAccessToken();
		boolean isCheck = false;
		
//		   "touser": "UserID1|UserID2|UserID3",
//		   "toparty": " PartyID1 | PartyID2 ",
//		   "totag": " TagID1 | TagID2 ",
//		   "agentid": 1,
//		   "text": "test",
//		   "safe":0
		
		
		String msgtype = Message_Text; 
		
		String touser = jsonParam.getString("touser");       
		String toparty = jsonParam.getString("toparty");     
		String totag = jsonParam.getString("totag");   
		String agentid = jsonParam.getString("agentid");            
		String text = jsonParam.getString("text");                
		String safe = jsonParam.getString("safe"); 
		
		JSONObject info = new JSONObject();

		JSONObject content = new JSONObject();
		content.put("content", text);
		
		info.put("touser", touser);
		info.put("toparty", toparty);
		info.put("totag", totag);
		info.put("msgtype", msgtype);
		info.put("agentid", agentid);
		info.put("text",content);
		info.put("safe", safe);
		
		
		String param = info.toJSONString();
		log.info("IMessageService -- sendText  param=" + param);
		
		
		String result = super.sendMessage(access_token, param,isCheck);
		return result;
	}
	
	
	public String sendNews(InstanceResource instanceResource,JSONObject jsonParam) throws Exception{
		log.info("IMessageService -- sendNews  jsonParam=" + jsonParam);
		String access_token = accessTokenService.getAccessToken(instanceResource).getAccessToken();
		boolean isCheck = false;
		
//		   "touser": "UserID1|UserID2|UserID3",
//		   "toparty": " PartyID1 | PartyID2 ",
//		   "totag": " TagID1 | TagID2 ",
//		   "agentid": 1,
//		   "articles":[
//		           {
//		               "title": "Title",
//		               "description": "Description",
//		               "url": "URL",
//		               "picurl": "PIC_URL"
//		           },
//		           {
//		               "title": "Title",
//		               "description": "Description",
//		               "url": "URL",
//		               "picurl": "PIC_URL"
//		           }    
//		       ]
		
		String msgtype = Message_News; 
		
		String touser = jsonParam.getString("touser");       
		String toparty = jsonParam.getString("toparty");     
		String totag = jsonParam.getString("totag");   
		String agentid = jsonParam.getString("agentid"); 
		JSONArray articlesArray = jsonParam.getJSONArray("articles"); 
		
		JSONObject art = new JSONObject();
		art.put("articles", articlesArray);
		
		JSONObject info = new JSONObject();
		
		info.put("touser", touser);
		info.put("toparty", toparty);
		info.put("totag", totag);
		info.put("msgtype", msgtype);
		info.put("agentid", agentid);
		info.put("news", art);
		
		
		String param = info.toJSONString();
		log.info("IMessageService -- sendNews  param=" + param);
		
		String result = super.sendMessage(access_token, param,isCheck);
		return result;
		
	}


}
